package com.hippy.erya.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hippy.erya.base.auth.context.LoginContextHolder;
import com.hippy.erya.entity.DishDO;
import com.hippy.erya.mapper.DishMapper;
import com.hippy.erya.query.BaseQueryWrapper;
import com.hippy.erya.sys.core.constant.cache.Cache;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, DishDO> {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private RawMaterialDictionaryServiceImpl rawMaterialDictionaryService;

    public Page<DishDO> queryByPage(long currentPage, int limit, String dishName, String dishCode) {
        Page<DishDO> page = new Page<>(currentPage, limit);
        QueryWrapper<DishDO> queryWrapper = BaseQueryWrapper.buildQuery();
        if (StringUtils.isNotBlank(dishName)) {
            queryWrapper.like("DISH_NAME", dishName);
        }
        if (StringUtils.isNotBlank(dishCode)) {
            queryWrapper.eq("DISH_CODE", dishCode);
        }
        Page<DishDO> pageResult = dishMapper.selectPage(page, queryWrapper);
        Map<String, String> map = rawMaterialDictionaryService.codeToName(null);
        pageResult.getRecords().forEach(dishDO -> {
            BaseQueryWrapper.outputMessage(dishDO);
            String dishMaterialList = dishDO.getDishMaterialList();
            String[] array = StringUtils.split(dishMaterialList, ";");
            StringJoiner joiner = new StringJoiner(";");
            if (ArrayUtils.isNotEmpty(array)) {
                for (String code : array) {
                    String value = map.getOrDefault(code, StringUtils.EMPTY);
                    if (StringUtils.isNotEmpty(value)) {
                        joiner.add(value);
                    }
                }
            }
            dishDO.setDishMaterialNameList(joiner.toString());
        });
        return pageResult;
    }

    public void insert(DishDO costingDO) {
        dishMapper.insert(costingDO);
    }


    public DishDO queryById(long id) {
        return dishMapper.selectById(id);
    }

    public void deleteById(long id) {
        DishDO dishDO = dishMapper.selectById(id);
        dishDO.deleteData(String.valueOf(LoginContextHolder.getContext().getUserId()));
        dishMapper.deleteById(id);
    }

    public DishDO queryByDishName(String userCode, String dishName) {
        QueryWrapper<DishDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USABLE_FLAG", Cache.USABLE);
        queryWrapper.eq("USER_CODE", userCode);
        queryWrapper.eq("DISH_NAME", dishName);
        return dishMapper.selectOne(queryWrapper);
    }

    public List<DishDO> queryByDishName(String dishName) {
        QueryWrapper<DishDO> queryWrapper = BaseQueryWrapper.buildQuery();
        queryWrapper.eq("DISH_NAME", dishName);
        return dishMapper.selectList(queryWrapper);
    }

}
